package cuongnh.project.japanesediary.vocabulary.model;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Table(name="tbl_context")
public class Context extends AbstractModelObject{

	@Id
	@Column(name = "context_id")
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="context_seq")
	@SequenceGenerator(
			name="context_seq",
			sequenceName="context_sequence",
			allocationSize=1
		)
	private Long contextId;
	
	@Column(name="context", length=500)
	private String context;
	
	@Column(name = "description", length = 200)
	private String description;

	@ManyToOne
	@JoinColumn(name = "parent_context_id")
	private Context parentContext;
	
	@OneToMany(cascade = {CascadeType.ALL}, mappedBy = "parentContext", fetch = FetchType.LAZY)
	private Set<Context> subContext = new HashSet<Context>();
	
	@ManyToMany(cascade = {CascadeType.ALL}, fetch = FetchType.LAZY)
	@JoinTable(name = "tbl_context_text", joinColumns = { @JoinColumn(name = "context_id") }, inverseJoinColumns = { @JoinColumn(name = "text_id") })
	private Set<Text> texts = new HashSet<Text>();
	
	@Column(name = "created_date")
	private Date createdDate;

	@Column(name = "modified_date")
	private Date modifiedDate;
	
	public Long getContextId() {
		return contextId;
	}

	public void setContextId(Long contextId) {
		this.contextId = contextId;
	}

	public String getContext() {
		return context;
	}

	public void setContext(String context) {
		this.context = context;
	}
	
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		String oldValue = this.description;
		this.description = description;
		firePropertyChange("description", oldValue, description);
	}

	public Context getParentContext() {
		return parentContext;
	}

	public void setParentContext(Context parentContext) {
		Context oldValue = this.parentContext;
		this.parentContext = parentContext;
		firePropertyChange("parentContext", oldValue, parentContext);
	}
	
	public Set<Context> getSubContext() {
		return subContext;
	}

	public void setSubContext(Set<Context> subContext) {
		Set<Context> oldValue = this.subContext;
		this.subContext = subContext;
		firePropertyChange("subContext", oldValue, subContext);
	}

	public Set<Text> getTexts() {
		return texts;
	}

	public void setTexts(Set<Text> texts) {
		Set<Text> oldValue = this.texts;
		this.texts = texts;
		firePropertyChange("texts", oldValue, texts);
	}

	public Date getCreatedDate() {
		return createdDate;
	}

	public void setCreatedDate(Date createdDate) {
		this.createdDate = createdDate;
	}
	
	public Date getModifiedDate() {
		return modifiedDate;
	}

	public void setModifiedDate(Date modifiedDate) {
		this.modifiedDate = modifiedDate;
	}

	@Override
	public boolean equals(Object obj) {
		boolean sameSame = false;

		if (obj != null && obj instanceof Context) {
			sameSame = contextId.equals(((Context) obj).contextId);
		}

		return sameSame;
	}
	
	@Override
	public int hashCode() {
		return this.contextId.intValue();
	}
	
}
